﻿Imports HMSDataLayer
Imports HMSLibrary

Public Class LoginWindow

    Private Sub CloseButton_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles CloseButton.Click
        Me.Close()
    End Sub

    Private Sub LoginWindow_Loaded(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles Me.Loaded
        UsernameTextBox.Focus()
    End Sub

    Private Sub LoginWindow_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Input.MouseButtonEventArgs) Handles Me.MouseDown
        DragMove()
    End Sub

    Private Function LoginValidation() As Boolean
        LoginValidation = False
        If UsernameTextBox.Text.Trim = "" Then
            CommonMessages.ShowValidationMsg("Please enter user name!")
            Return False
        End If

        If UserPasswordBox.Password = "" Then
            CommonMessages.ShowValidationMsg("Please enter password!")
            Return False
        End If
        Return True
    End Function

    Private Sub LoginButton_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles LoginButton.Click
        If LoginValidation() = False Then Exit Sub
        Dim dataProvider As New MasterDataLayer()
        AppCommon.loginInfo = dataProvider.ValidateUser(UsernameTextBox.Text, UserPasswordBox.Password)
        If AppCommon.loginInfo.UserID = 0 Then Exit Sub
        Dim MainWin As New MainWindow
        MainWin.Show()
        Me.Close()
    End Sub

    Private Sub UserPasswordBox_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Input.KeyEventArgs) Handles UserPasswordBox.KeyDown, UsernameTextBox.KeyDown
        If e.Key = Input.Key.Enter Then
            LoginButton_Click(Nothing, Nothing)
        End If
    End Sub
End Class
